// 安装依赖
// npm install jspdf html2canvas

import { ref } from 'vue'
import jsPDF from 'jspdf'
import html2canvas from 'html2canvas'

export function usePDFGenerator() {
    const generatePDF = async (urls) => {
        const pdf = new jsPDF('p', 'mm', 'a4')

        for (const url of urls) {
            try {
                if (url.endsWith('.jpg') || url.endsWith('.png')) {
                    await addImageToPDF(pdf, url)
                } else if (url.endsWith('.html')) {
                    await addHTMLToPDF(pdf, url)
                } else if (url.endsWith('.docx')) {
                    await addDocxToPDF(pdf, url)
                } else if (url.endsWith('.pdf')) {
                    await addPdfToPDF(pdf, url)
                }
            } catch (error) {
                console.error(`处理文件 ${url} 失败:`, error)
                // 可以添加错误页或继续处理下一个文件
            }
        }

        const pdfBlob = pdf.output('blob')
        printPDF(pdfBlob)
    }

    const addPdfToPDF = async (pdf, pdfUrl) => {
        const response = await fetch(pdfUrl)
        const existingPdfBytes = await response.arrayBuffer()
        const pdfDoc = await PDFDocument.load(existingPdfBytes)

        const pages = await pdfDoc.copyPages(pdfDoc, pdfDoc.getPageIndices())
        pages.forEach(page => {
            pdf.addPage(page)
        })
    }

    const addDocxToPDF = async (pdf, docxUrl) => {
        const response = await fetch(docxUrl)
        const docxBlob = await response.blob()

        // 简单方法：显示提示信息代替DOCX内容
        pdf.text('DOCX文件内容: ' + docxUrl, 10, 10)
        pdf.addPage()

        // 或者使用更复杂的转换库如mammoth.js
        // const { value: html } = await mammoth.convertToHtml({ arrayBuffer: await docxBlob.arrayBuffer() })
        // 然后将HTML转换为PDF
    }

    // ...保留其他方法不变...

    return { generatePDF }
}

// utils/docxToPdf.js
import { renderAsync } from 'docx-preview'
import { PDFDocument, rgb } from 'pdf-lib'

export async function convertDocxToPdf(docxBlob) {
    // 1. 将DOCX渲染到虚拟DOM
    const container = document.createElement('div')
    container.style.visibility = 'hidden'
    container.style.position = 'absolute'
    document.body.appendChild(container)

    await renderAsync(docxBlob, container)

    // 2. 获取渲染后的HTML
    const htmlContent = container.innerHTML
    console.log(htmlContent)
    document.body.removeChild(container)

    // 3. 使用html2canvas转换为图片
    const tempDiv = document.createElement('div')
    tempDiv.innerHTML = htmlContent
    document.body.appendChild(tempDiv)
    console.log({ tempDiv })
    const canvas = await html2canvas(tempDiv)
    document.body.removeChild(tempDiv)

    // 4. 创建PDF并添加图片
    const pdfDoc = await PDFDocument.create()
    const page = pdfDoc.addPage([canvas.width, canvas.height])

    const pngImage = await pdfDoc.embedPng(canvas.toDataURL('image/png'))
    console.log({ pngImage })
    page.drawImage(pngImage, {
        x: 0,
        y: 0,
        width: canvas.width,
        height: canvas.height,
    })
    console.log({ page })
    return await pdfDoc.save()
}